package xyz.maxwells.bigdata.ctrl;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import xyz.maxwells.bigdata.domain.RequestResult;
import xyz.maxwells.bigdata.domain.SysUser;
import xyz.maxwells.bigdata.domain.VuePageable;
import xyz.maxwells.bigdata.domain.data.Upload;
import xyz.maxwells.bigdata.domain.data.UploadListDTO;
import xyz.maxwells.bigdata.exception.BigdataException;
import xyz.maxwells.bigdata.service.UploadService;

import java.io.*;

/**
 * Created by wuzusheng on 2018/5/16.
 */
@CrossOrigin
@RestController
public class UploadCtrl {
    @Autowired
    private UploadService service;
    //上传
    @RequestMapping("/upload")
    public RequestResult upload(MultipartFile file)throws Exception{

        if (!file.getOriginalFilename().endsWith(".xls")){
            throw new BigdataException("bigdata_05_002::不支持该文件类型");
        }
        if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String){
            throw new BigdataException("bigdata_01_006::尚未登录,无法获取当前用户");
        }
        SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (!file.isEmpty()){
            String path = "G:\\毕业设计\\bigdataview\\static\\excellfile";
            System.out.println("上传文件的路径："+path);
            String filename = file.getOriginalFilename();
            File filepath = new File(path,filename);
            if(!filepath.getParentFile().exists()){
                filepath.getParentFile().mkdirs();
            }
            file.transferTo(new File(path + File.separator + filename));
            Upload upload = new Upload();
            upload.setUserName(user.getUsername());
            upload.setFileName(file.getOriginalFilename());
            service.save(upload);
        }else {
            throw new BigdataException("bigdata_05_001::上传的文件不能为空");
        }

        return new RequestResult("上传成功");
    }
    //查询我的文件
    @RequestMapping("/myfile")
    public RequestResult myfile()throws BigdataException{
        if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String){
            throw new BigdataException("bigdata_01_006::尚未登录,无法获取当前用户");
        }
        SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return new RequestResult(service.findByUserName(user.getUsername()));
    }
    //删除文件
    @RequestMapping("/deletefile")
    public RequestResult deleteFile(Long id,String filename)throws BigdataException{
        if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String){
            throw new BigdataException("bigdata_01_006::尚未登录,无法获取当前用户");
        }
        SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String path = "G:\\毕业设计\\bigdataview\\static\\excellfile\\";
        File file = new File(path+filename);
        if (file.exists()){
            file.delete();
        }
        service.delete(id);
        return new RequestResult();
    }
    //返回展示数据二维数组
    @RequestMapping("/getdisplay")
    public RequestResult getDisplayData(String filename) throws BigdataException{
        try {
            InputStream is = new FileInputStream("G:\\毕业设计\\bigdataview\\static\\excellfile\\"+filename);
            Workbook wb = Workbook.getWorkbook(is);
            Sheet sheet = wb.getSheet(0);
            int row =  sheet.getRows();
            int column = sheet.getColumns();
            String[][] str = new String[row][column];
            for (int i =0;i < row;i++){
                for (int j = 0;j < column;j++){
                    str[i][j] = sheet.getCell(j,i).getContents();
                }
            }
            return new RequestResult(str);
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (BiffException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }
    //查询上传文件信息
    @RequestMapping("/filelist")
    public RequestResult lsit(UploadListDTO dto, VuePageable pageable){
        Page<Upload> page = service.list(UploadListDTO.getSpecification(dto),pageable.getPageable());
        return new RequestResult(page);
    }
}
